<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Teacher extends Model
{
    //
    use SoftDeletes;

    public $table = 'teachers';

    public $fillable = [
        'name',
        'mobile',
        'code',
        'id_number',
        'recommend',
        'profile',
        'photo',

    ];

    public function user()
    {
        return $this->hasOne(User::class, 'teacher_id', 'id');
    }

    public function courses(){
        return $this->belongsToMany(Course::class, 'teacher_has_courses', 'teacher_id', 'course_id')->withTrashed();

    }

    public function getCourseId(){
        return DB::table('teacher_has_courses')->select('course_id')->where('teacher_id',$this->id)->pluck('course_id');

    }

    public function getCoursesView(){
        $names = self::with('courses')->where('id',$this->id)->first();
        $show = '';
        foreach ($names->courses as $name){
            $show = $name->name.'讲师 ';
            break;
        }
        if(count($names->courses) >1){
            $show .='等';
        }
        return $show;
    }

}
